; ############################     macro     ##############################


; setBank
; -------
; A    -> bit 22-15 of rom address to set in the bank register
; HL  <-  BANKREG
;
; set the bank register for ROM area
; = 101 cycles

            macro   setBank

            LD  HL, BANKREG         ; HL = BANKREG              ' 10
            LD  (HL), A             ; #1 (bit 15)               ' 7
            RRA                     ;                           ' 4
            LD  (HL), A             ; #2 (bit 16)               ' 7
            RRA                     ;                           ' 4
            LD  (HL), A             ; #3 (bit 17)               ' 7
            RRA                     ;                           ' 4
            LD  (HL), A             ; #4 (bit 18)               ' 7
            RRA                     ;                           ' 4
            LD  (HL), A             ; #5 (bit 19)               ' 7
            RRA                     ;                           ' 4
            LD  (HL), A             ; #6 (bit 20)               ' 7
            RRA                     ;                           ' 4
            LD  (HL), A             ; #7 (bit 21)               ' 7
            RRA                     ;                           ' 4
            LD  (HL), A             ; #8 (bit 22)               ' 7
            LD  (HL), L             ; #9 (bit 23 = 0)           ' 7

            endm                    ;                           ' 101


; setBankFast
; -----------
; A    -> bit 21-15 of rom address to set in the bank register
; HL  <-  BANKREG
;
; set the bank register for ROM area
; = 97 cycles

            macro   setBankFast

            LD  HL, BANKREG         ; HL = BANKREG              ' 10
            LD  (HL), A             ; #1 (bit 15)               ' 7
            RRA                     ;                           ' 4
            LD  (HL), A             ; #2 (bit 16)               ' 7
            RRA                     ;                           ' 4
            LD  (HL), A             ; #3 (bit 17)               ' 7
            RRA                     ;                           ' 4
            LD  (HL), A             ; #4 (bit 18)               ' 7
            RRA                     ;                           ' 4
            LD  (HL), A             ; #5 (bit 19)               ' 7
            RRA                     ;                           ' 4
            LD  (HL), A             ; #6 (bit 20)               ' 7
            RRA                     ;                           ' 4
            LD  (HL), A             ; #7 (bit 21)               ' 7
            LD  (HL), L             ; #8 (bit 22 = 0)           ' 7
            LD  (HL), L             ; #9 (bit 23 = 0)           ' 7

            endm                    ;                           ' 97


; setBank_BC
; ----------
; A    -> bit 22-15 of rom address to set in the bank register
; BC  <-  BANKREG
; A   <-  0
;
; set the bank register for ROM area
; = 105 cycles

            macro   setBank_BC

            LD  BC, BANKREG         ; BC = BANKREG              ' 10
            LD  (BC), A             ; #1 (bit 15)               ' 7
            RRA                     ;                           ' 4
            LD  (BC), A             ; #2 (bit 16)               ' 7
            RRA                     ;                           ' 4
            LD  (BC), A             ; #3 (bit 17)               ' 7
            RRA                     ;                           ' 4
            LD  (BC), A             ; #4 (bit 18)               ' 7
            RRA                     ;                           ' 4
            LD  (BC), A             ; #5 (bit 19)               ' 7
            RRA                     ;                           ' 4
            LD  (BC), A             ; #6 (bit 20)               ' 7
            RRA                     ;                           ' 4
            LD  (BC), A             ; #7 (bit 21)               ' 7
            RRA                     ;                           ' 4
            LD  (BC), A             ; #8 (bit 22)               ' 7
            RRA                     ;                           ' 4
            LD  (BC), A             ; #9 (bit 23 = 0)           ' 7

            endm                    ;                           ' 105


; setBank_DE
; ----------
; A    -> bit 22-15 of rom address to set in the bank register
; DE  <-  BANKREG
; A   <-  0
;
; set the bank register for ROM area
; = 105 cycles

            macro   setBank_DE

            LD  DE, BANKREG         ; DE = BANKREG              ' 10
            LD  (DE), A             ; #1 (bit 15)               ' 7
            RRA                     ;                           ' 4
            LD  (DE), A             ; #2 (bit 16)               ' 7
            RRA                     ;                           ' 4
            LD  (DE), A             ; #3 (bit 17)               ' 7
            RRA                     ;                           ' 4
            LD  (DE), A             ; #4 (bit 18)               ' 7
            RRA                     ;                           ' 4
            LD  (DE), A             ; #5 (bit 19)               ' 7
            RRA                     ;                           ' 4
            LD  (DE), A             ; #6 (bit 20)               ' 7
            RRA                     ;                           ' 4
            LD  (DE), A             ; #7 (bit 21)               ' 7
            RRA                     ;                           ' 4
            LD  (DE), A             ; #8 (bit 22)               ' 7
            RRA                     ;                           ' 4
            LD  (DE), A             ; #9 (bit 23 = 0)           ' 7

            endm                    ;                           ' 105


; writeYM
; -------
; HL   -> YM port where to write
; D    -> address
; E    -> value
;
; write to YM2612

            macro   writeYM

.wait0
            BIT     7, (HL)         ; test YM not busy
            JR      NZ, .wait0
            LD      (HL), D         ; write address
.wait1
            BIT     7, (HL)         ; test YM not busy
            JR      NZ, .wait1
            INC     L
            LD      (HL), E         ; write value

            endm


; getPlayCommand
; --------------
; HL  <-  point to COMMAND
; zf  <-  channel 'ch' play command
;
; Z flag reset if play command for channel 'ch'
; = 22 cycles

            macro   getPlayCommand ch

            LD      HL, COMMAND             ;                   ' 10
            BIT     COMPLAY_SFT+ch, (HL)    ; play command ?    ' 12

            endm                            ;                   ' 22


; getStopCommand
; --------------
; HL  <-  point to COMMAND
; zf  <-  channel 'ch' stop command
;
; Z flag reset if stop command for channel 'ch'
; = 22 cycles

            macro   getStopCommand ch

            LD      HL, COMMAND             ;                   ' 10
            BIT     COMSTOP_SFT+ch, (HL)    ; stop command ?    ' 12

            endm                            ;                   ' 22


; setPlayCommand
; ----------------
; HL  <-  point to COMMAND
;
; set the play command for channel 'ch'
; = 25 cycles

            macro   setPlayCommand ch

            LD      HL, COMMAND             ;                   ' 10
            SET     COMPLAY_SFT+ch, (HL)    ; set command       ' 15

            endm                            ;                   ' 25


; setStopCommand
; ----------------
; HL  <-  point to COMMAND
;
; set the stop command for channel 'ch'
; = 25 cycles

            macro   setStopCommand ch

            LD      HL, COMMAND             ;                   ' 10
            SET     COMSTOP_SFT+ch, (HL)    ; set command       ' 15

            endm                            ;                   ' 25


; clearPlayCommand
; ----------------
; HL  <-  point to COMMAND
;
; clear the play command for channel 'ch'
; = 25 cycles

            macro   clearPlayCommand ch

            LD      HL, COMMAND             ;                   ' 10
            RES     COMPLAY_SFT+ch, (HL)    ; clear command     ' 15

            endm                            ;                   ' 25


; clearStopCommand
; ----------------
; HL  <-  point to COMMAND
;
; clear the stop command for channel 'ch'
; = 25 cycles

            macro   clearStopCommand ch

            LD      HL, COMMAND             ;                   ' 10
            RES     COMSTOP_SFT+ch, (HL)    ; clear command     ' 15

            endm                            ;                   ' 25


; getPlayStatus
; -------------
; HL  <- point to STATUS
; zf  <-  channel 'ch' is playing
;
; Z flag reset if play status for channel 'ch'
; = 22 cycles

            macro   getPlayStatus ch

            LD      HL, STATUS              ;                   ' 10
            BIT     STATPLAY_SFT+ch, (HL)   ; get status        ' 12

            endm                            ;                   ' 22


; setPlayStatus
; -------------
; HL  <- point to STATUS
;
; set the play status for channel 'ch'
; = 25 cycles

            macro   setPlayStatus ch

            LD      HL, STATUS              ;                   ' 10
            SET     STATPLAY_SFT+ch, (HL)   ; set status        ' 15

            endm                            ;                   ' 25


; clearPlayStatus
; ---------------
; HL  <- point to STATUS
;
; clear play status for channel 'ch'
; = 25 cycles

            macro   clearPlayStatus ch

            LD      HL, STATUS              ;                   ' 10
            RES     STATPLAY_SFT+ch, (HL)   ; clear status      ' 15

            endm                            ;                   ' 25


; getLoopStatus
; -------------
; HL  <- point to STATUS+1
; zf  <-  channel 'ch' is playing
;
; Z flag reset if loop status for channel 'ch'
; = 22 cycles

            macro   getLoopStatus ch

            LD      HL, STATUS+1            ;                   ' 10
            BIT     STATPLAY_SFT+ch, (HL)   ; get status        ' 12

            endm                            ;                   ' 22


; setLoopStatus
; -------------
; HL  <- point to STATUS+1
;
; set the loop status for channel 'ch'
; = 25 cycles

            macro   setLoopStatus ch

            LD      HL, STATUS+1            ;                   ' 10
            SET     STATPLAY_SFT+ch, (HL)   ; set status        ' 15

            endm                            ;                   ' 25


; clearLoopStatus
; ---------------
; HL  <- point to STATUS+1
;
; clear loop status for channel 'ch'
; = 25 cycles

            macro   clearLoopStatus ch

            LD      HL, STATUS+1            ;                   ' 10
            RES     STATPLAY_SFT+ch, (HL)   ; clear status      ' 15

            endm                            ;                   ' 25


; clearAllStatus
; --------------
; HL  <- 0
;
; clear play and loop status for all channel
; = 26 cycles

            macro   clearAllStatus

            LD      HL, $00                 ;                   ' 10
            LD      (STATUS), HL            ; clear status      ' 16

            endm                            ;                   ' 26


; ############################  macro wait macro  ##############################


; wait2p16x
; ---------
; wait for ((w * 16) + 2) cycles
;
            macro   wait2p16x w

            LD      A, w            ; 7-2
.loop
            DEC     A               ; 4
            JR      NZ, .loop       ; 12

            endm


            macro   wait4
            NOP
            endm

            macro   wait6
            INC     DE
            endm

            macro   wait7
            LD      A, (DE)
            endm

            macro   wait8
            wait4
            wait4
            endm

            macro   wait9
            LD      R, A
            endm

            macro   wait10
            JP      .loop
.loop
            endm

            macro   wait11
            wait7
            wait4
            endm

            macro   wait12
            JR      .loop
.loop
            endm

            macro   wait13
            wait9
            wait4
            endm

            macro   wait14
            wait7
            wait7
            endm

            macro   wait15
            wait8
            wait7
            endm

            macro   wait16
            wait12
            wait4
            endm

            macro   wait17
            wait10
            wait7
            endm

            macro   wait18
            wait9
            wait9
            endm

            macro   wait19
            wait12
            wait7
            endm

            macro   wait20
            wait11
            wait9
            endm

            macro   wait21
            wait12
            wait9
            endm

            macro   wait22
            wait18
            wait4
            endm

            macro   wait23
            wait14
            wait9
            endm

            macro   wait24
            wait12
            wait12
            endm

            macro   wait25
            wait18
            wait7
            endm

            macro   wait26
            wait18
            wait8
            endm

            macro   wait27
            wait18
            wait9
            endm

            macro   wait28
            wait21
            wait7
            endm

            macro   wait29
            wait21
            wait8
            endm

            macro   wait30
            wait21
            wait9
            endm

            macro   wait31
            wait21
            wait10
            endm

            macro   wait32
            wait20
            wait12
            endm

            macro   wait33
            wait21
            wait12
            endm

            macro   wait34
            wait2p16x 2
            endm

            macro   wait35
            wait28
            wait7
            endm

            macro   wait36
            wait27
            wait9
            endm

            macro   wait37
            wait28
            wait9
            endm

            macro   wait38
            wait27
            wait11
            endm

            macro   wait39
            wait27
            wait12
            endm

            macro   wait40
            wait27
            wait13
            endm

            macro   wait41
            wait34
            wait7
            endm

            macro   wait42
            wait34
            wait8
            endm

            macro   wait43
            wait36
            wait7
            endm

            macro   wait44
            wait34
            wait10
            endm

            macro   wait45
            wait36
            wait9
            endm

            macro   wait46
            wait34
            wait12
            endm

            macro   wait47
            wait34
            wait13
            endm

            macro   wait48
            wait36
            wait12
            endm

            macro   wait49
            wait36
            wait13
            endm

            macro   wait50
            wait2p16x 3
            endm

            macro   wait51
            wait34
            wait17
            endm

            macro   wait52
            wait34
            wait18
            endm

            macro   wait53
            wait36
            wait17
            endm

            macro   wait54
            wait50
            wait4
            endm

            macro   wait55
            wait34
            wait21
            endm

            macro   wait56
            wait34
            wait22
            endm

            macro   wait57
            wait50
            wait7
            endm

            macro   wait58
            wait50
            wait8
            endm

            macro   wait59
            wait50
            wait9
            endm

            macro   wait60
            wait50
            wait10
            endm

            macro   wait61
            wait50
            wait11
            endm

            macro   wait62
            wait50
            wait12
            endm

            macro   wait63
            wait50
            wait13
            endm

            macro   wait64
            wait50
            wait14
            endm

            macro   wait65
            wait50
            wait15
            endm

            macro   wait66
            wait2p16x 4
            endm

            macro   wait67
            wait50
            wait17
            endm

            macro   wait68
            wait50
            wait18
            endm

            macro   wait70
            wait66
            wait4
            endm

            macro   wait72
            wait50
            wait22
            endm

            macro   wait73
            wait66
            wait7
            endm

            macro   wait74
            wait66
            wait8
            endm

            macro   wait75
            wait66
            wait9
            endm

            macro   wait76
            wait66
            wait10
            endm

            macro   wait77
            wait66
            wait11
            endm

            macro   wait78
            wait66
            wait12
            endm

            macro   wait79
            wait66
            wait13
            endm

            macro   wait80
            wait66
            wait14
            endm

            macro   wait81
            wait66
            wait15
            endm

            macro   wait82
            wait2p16x 5
            endm

            macro   wait83
            wait66
            wait17
            endm

            macro   wait84
            wait66
            wait18
            endm

            macro   wait85
            wait66
            wait19
            endm

            macro   wait86
            wait82
            wait4
            endm

            macro   wait87
            wait66
            wait21
            endm

            macro   wait88
            wait66
            wait22
            endm

            macro   wait89
            wait82
            wait7
            endm

            macro   wait90
            wait82
            wait8
            endm

            macro   wait91
            wait82
            wait9
            endm

            macro   wait92
            wait82
            wait10
            endm

            macro   wait94
            wait82
            wait12
            endm

            macro   wait95
            wait82
            wait13
            endm

            macro   wait96
            wait82
            wait14
            endm

            macro   wait97
            wait82
            wait15
            endm

            macro   wait98
            wait2p16x 6
            endm

            macro   wait99
            wait82
            wait17
            endm

            macro   wait100
            wait82
            wait18
            endm

            macro   wait102
            wait98
            wait4
            endm

            macro   wait103
            wait82
            wait21
            endm

            macro   wait104
            wait82
            wait22
            endm

            macro   wait105
            wait98
            wait7
            endm

            macro   wait106
            wait98
            wait8
            endm

            macro   wait107
            wait98
            wait9
            endm

            macro   wait108
            wait98
            wait10
            endm

            macro   wait109
            wait98
            wait11
            endm

            macro   wait110
            wait98
            wait12
            endm

            macro   wait111
            wait98
            wait13
            endm

            macro   wait112
            wait98
            wait14
            endm

            macro   wait114
            wait2p16x 7
            endm

            macro   wait115
            wait98
            wait17
            endm

            macro   wait116
            wait98
            wait18
            endm

            macro   wait117
            wait98
            wait19
            endm

            macro   wait118
            wait114
            wait4
            endm

            macro   wait119
            wait98
            wait21
            endm

            macro   wait120
            wait98
            wait22
            endm

            macro   wait121
            wait114
            wait7
            endm

            macro   wait122
            wait114
            wait8
            endm

            macro   wait123
            wait114
            wait9
            endm

            macro   wait124
            wait114
            wait10
            endm

            macro   wait125
            wait114
            wait11
            endm

            macro   wait126
            wait114
            wait12
            endm

            macro   wait127
            wait114
            wait13
            endm

            macro   wait128
            wait114
            wait14
            endm

            macro   wait129
            wait114
            wait15
            endm

            macro   wait130
            wait2p16x 8
            endm

            macro   wait131
            wait114
            wait17
            endm

            macro   wait132
            wait114
            wait18
            endm

            macro   wait133
            wait114
            wait19
            endm

            macro   wait134
            wait130
            wait4
            endm

            macro   wait135
            wait114
            wait21
            endm

            macro   wait136
            wait114
            wait22
            endm

            macro   wait137
            wait130
            wait7
            endm

            macro   wait138
            wait130
            wait8
            endm

            macro   wait139
            wait130
            wait9
            endm

            macro   wait140
            wait130
            wait10
            endm

            macro   wait141
            wait130
            wait11
            endm

            macro   wait144
            wait130
            wait14
            endm

            macro   wait145
            wait130
            wait15
            endm

            macro   wait146
            wait2p16x 9
            endm

            macro   wait147
            wait130
            wait17
            endm

            macro   wait148
            wait130
            wait18
            endm

            macro   wait149
            wait130
            wait19
            endm

            macro   wait150
            wait146
            wait4
            endm

            macro   wait154
            wait146
            wait8
            endm

            macro   wait155
            wait146
            wait9
            endm

            macro   wait156
            wait146
            wait10
            endm

            macro   wait157
            wait146
            wait11
            endm

            macro   wait158
            wait146
            wait12
            endm

            macro   wait160
            wait146
            wait14
            endm

            macro   wait161
            wait146
            wait15
            endm

            macro   wait162
            wait2p16x 10
            endm

            macro   wait164
            wait146
            wait18
            endm

            macro   wait165
            wait146
            wait19
            endm

            macro   wait166
            wait162
            wait4
            endm

            macro   wait172
            wait162
            wait10
            endm

            macro   wait174
            wait162
            wait12
            endm

            macro   wait175
            wait162
            wait13
            endm

            macro   wait176
            wait162
            wait14
            endm

            macro   wait178
            wait2p16x 11
            endm

            macro   wait179
            wait162
            wait17
            endm

            macro   wait180
            wait162
            wait18
            endm

            macro   wait182
            wait178
            wait4
            endm

            macro   wait183
            wait162
            wait121
            endm

            macro   wait186
            wait178
            wait8
            endm

            macro   wait187
            wait178
            wait9
            endm

            macro   wait189
            wait178
            wait11
            endm

            macro   wait191
            wait178
            wait13
            endm

            macro   wait192
            wait178
            wait14
            endm

            macro   wait193
            wait178
            wait15
            endm

            macro   wait194
            wait2p16x 12
            endm

            macro   wait197
            wait178
            wait19
            endm

            macro   wait203
            wait194
            wait9
            endm

            macro   wait210
            wait2p16x 13
            endm

            macro   wait226
            wait2p16x 14
            endm

            macro   wait234
            wait226
            wait8
            endm

            macro   wait242
            wait2p16x 15
            endm

            macro   wait244
            wait226
            wait18
            endm

            macro   wait246
            wait242
            wait4
            endm

            macro   wait254
            wait242
            wait12
            endm

            macro   wait258
            wait2p16x 16
            endm

            macro   wait260
            wait242
            wait18
            endm

            macro   wait262
            wait258
            wait4
            endm

            macro   wait264
            wait242
            wait22
            endm

            macro   wait268
            wait258
            wait10
            endm

            macro   wait274
            wait2p16x 17
            endm

            macro   wait276
            wait258
            wait18
            endm

            macro   wait278
            wait274
            wait4
            endm

            macro   wait282
            wait274
            wait8
            endm

            macro   wait283
            wait274
            wait9
            endm

            macro   wait285
            wait274
            wait11
            endm

            macro   wait287
            wait274
            wait13
            endm

            macro   wait290
            wait2p16x 18
            endm

            macro   wait294
            wait290
            wait4
            endm

            macro   wait295
            wait274
            wait21
            endm

            macro   wait298
            wait290
            wait8
            endm

            macro   wait305
            wait290
            wait15
            endm

            macro   wait306
            wait2p16x 19
            endm

            macro   wait308
            wait290
            wait18
            endm

            macro   wait320
            wait306
            wait14
            endm

            macro   wait322
            wait2p16x 20
            endm

            macro   wait324
            wait306
            wait18
            endm

            macro   wait329
            wait322
            wait7
            endm

            macro   wait337
            wait322
            wait15
            endm

            macro   wait338
            wait2p16x 21
            endm

            macro   wait345
            wait338
            wait7
            endm

            macro   wait434
            wait2p16x 27
            endm

            macro   wait450
            wait2p16x 28
            endm

            macro   wait454
            wait450
            wait4
            endm

            macro   wait455
            wait434
            wait21
            endm

            macro   wait546
            wait2p16x 34
            endm

            macro   wait562
            wait2p16x 35
            endm

            macro   wait567
            wait546
            wait21
            endm

            macro   wait578
            wait2p16x 36
            endm

            macro   wait586
            wait578
            wait8
            endm

            macro   wait590
            wait578
            wait12
            endm

            macro   wait594
            wait2p16x 37
            endm

            macro   wait595
            wait578
            wait17
            endm

            macro   wait598
            wait594
            wait4
            endm

            macro   wait599
            wait578
            wait21
            endm

            macro   wait602
            wait594
            wait8
            endm

            macro   wait609
            wait594
            wait15
            endm

            macro   wait610
            wait2p16x 38
            endm

            macro   wait614
            wait610
            wait4
            endm

            macro   wait618
            wait610
            wait8
            endm

            macro   wait619
            wait610
            wait9
            endm

            macro   wait622
            wait610
            wait12
            endm

            macro   wait623
            wait610
            wait13
            endm

            macro   wait626
            wait2p16x 39
            endm

            macro   wait634
            wait626
            wait8
            endm

            macro   wait638
            wait626
            wait12
            endm
